package com.lz.ccjdemo.act;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.lz.ccjdemo.utils.network.NetStateChangeObserver;
import com.lz.ccjdemo.utils.network.NetStateChangeReceiver;
import com.lz.ccjdemo.utils.network.NetworkType;

public class NetStateActivity extends Activity implements NetStateChangeObserver {
    TextView tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv = new TextView(this);
        tv.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        tv.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        tv.setGravity(Gravity.CENTER);
        tv.setText("在当前页面切换网络环境可以检测网络状态变更");
        setContentView(tv);
        NetStateChangeReceiver.registerReceiver(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        NetStateChangeReceiver.registerObserver(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        NetStateChangeReceiver.unRegisterObserver(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NetStateChangeReceiver.unRegisterReceiver(this);
    }

    @Override
    public void onNetDisconnected() {
        //网络断开
        Toast.makeText(this, "当前网络已断开", Toast.LENGTH_SHORT).show();
        tv.setText("当前网络已断开");
    }

    @Override
    public void onNetConnected(NetworkType networkType) {
        //网络连接、切换
        String result="当前网络" + networkType.toString();
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        tv.setText(result);
        switch (networkType) {
            case NETWORK_WIFI:
                break;
            case NETWORK_4G:
                break;
            default:
                break;
        }
    }
}
